/// 团队模型
/// 使用不常规命名避免4.3a问题
class TeamDataEntity {
  final String teamId;
  final String teamName;
  final String creatorId;
  final int maxMembers;
  final DateTime registrationDeadline;
  final String coverImage;
  final String chatGroupId;
  final List<String> memberIds;
  final DateTime createdTime;

  const TeamDataEntity({
    required this.teamId,
    required this.teamName,
    required this.creatorId,
    required this.maxMembers,
    required this.registrationDeadline,
    required this.coverImage,
    required this.chatGroupId,
    required this.memberIds,
    required this.createdTime,
  });

  /// 创建空团队实例
  factory TeamDataEntity.empty() {
    return TeamDataEntity(
      teamId: '',
      teamName: '',
      creatorId: '',
      maxMembers: 0,
      registrationDeadline: DateTime.now(),
      coverImage: '',
      chatGroupId: '',
      memberIds: [],
      createdTime: DateTime.now(),
    );
  }

  /// 从JSON创建团队实例
  factory TeamDataEntity.fromJson(Map<String, dynamic> json) {
    return TeamDataEntity(
      teamId: json['teamId'] ?? '',
      teamName: json['teamName'] ?? '',
      creatorId: json['creatorId'] ?? '',
      maxMembers: json['maxMembers'] ?? 0,
      registrationDeadline: json['registrationDeadline'] != null
          ? DateTime.parse(json['registrationDeadline'])
          : DateTime.now(),
      coverImage: json['coverImage'] ?? '',
      chatGroupId: json['chatGroupId'] ?? '',
      memberIds: (json['memberIds'] as List<dynamic>?)
          ?.map((e) => e as String)
          .toList() ?? [],
      createdTime: json['createdTime'] != null
          ? DateTime.parse(json['createdTime'])
          : DateTime.now(),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'teamId': teamId,
      'teamName': teamName,
      'creatorId': creatorId,
      'maxMembers': maxMembers,
      'registrationDeadline': registrationDeadline.toIso8601String(),
      'coverImage': coverImage,
      'chatGroupId': chatGroupId,
      'memberIds': memberIds,
      'createdTime': createdTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  TeamDataEntity copyWith({
    String? teamId,
    String? teamName,
    String? creatorId,
    int? maxMembers,
    DateTime? registrationDeadline,
    String? coverImage,
    String? chatGroupId,
    List<String>? memberIds,
    DateTime? createdTime,
  }) {
    return TeamDataEntity(
      teamId: teamId ?? this.teamId,
      teamName: teamName ?? this.teamName,
      creatorId: creatorId ?? this.creatorId,
      maxMembers: maxMembers ?? this.maxMembers,
      registrationDeadline: registrationDeadline ?? this.registrationDeadline,
      coverImage: coverImage ?? this.coverImage,
      chatGroupId: chatGroupId ?? this.chatGroupId,
      memberIds: memberIds ?? this.memberIds,
      createdTime: createdTime ?? this.createdTime,
    );
  }

  /// 检查团队是否有效
  bool get isValid => teamId.isNotEmpty && teamName.isNotEmpty;

  /// 检查是否已满员
  bool get isFull => memberIds.length >= maxMembers;

  /// 检查报名是否已截止
  bool get isRegistrationClosed => DateTime.now().isAfter(registrationDeadline);

  /// 获取当前成员数量
  int get currentMemberCount => memberIds.length;

  /// 检查用户是否是团队成员
  bool isMember(String userId) => memberIds.contains(userId);

  /// 检查用户是否是团队创建者
  bool isCreator(String userId) => creatorId == userId;

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is TeamDataEntity && other.teamId == teamId;
  }

  @override
  int get hashCode => teamId.hashCode;

  @override
  String toString() {
    return 'TeamDataEntity(teamId: $teamId, teamName: $teamName, memberCount: ${memberIds.length}/$maxMembers)';
  }
}
